package com.woniuxy.three.pms.order.controller;

import java.io.IOException;

import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;

import org.springframework.web.bind.annotation.RestController;

import com.woniuxy.three.pms.util.WebSocketUtil;



@RestController
@ServerEndpoint("/wechat/{username}")
public class WebSoketController {
	private WebSocketUtil util= new WebSocketUtil();
	
		@OnOpen
		public void openmessage(@PathParam("username") String username,Session session) {
			util.ONLINE_USER.put(username, session);
			String mes="欢迎["+username+"]来到聊天室";
			//util.sendall(mes);
		}
		
		@OnMessage
		public void sendmessage(String orderNumber) {
			String mes="你有一个新订单,订单号为:"+orderNumber;
			util.sendall(mes);
		}
		
		@OnClose
		public void closemessage(@PathParam("username") String username,Session session) {
			util.ONLINE_USER.remove(username);
			String mes="["+username+"]离开了聊天室";
			util.sendall(mes);
			try {
				session.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		@OnError
		 public void onError(Session session, Throwable throwable) {
		 try {
		 session.close();
		 } catch (IOException e) {
			 System.out.println("关闭异常");
		 }
		}
}
